//
//  AIGirlProfileViewController.swift
//  AIGirl
//
//  Created by Rae on 2023/12/22.
//

import UIKit

private let headerID    = "AIGirlProfileHeaderView"

class AIGirlProfileViewController: AIGirlBaseViewController {

    private var navView = AIGirlProfileNavView()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(AIGirlProfileTableViewCell.self)
        tableView.register(AIGirlProfileHeaderView.self, forHeaderFooterViewReuseIdentifier: headerID)
        tableView.showsVerticalScrollIndicator = false
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            tableView
        }

        tableView.leading(0).trailing(0).top(.statusHeight).bottom(.safe_bottom)
        navView.frame = CGRect(x: 0, y: 0, width: .screenWidth, height: 310)
        tableView.tableHeaderView = navView
        navView.diamondClosure = {
            self.animation.animation(show: AIGirlMessagePremiumView())
        }
        NotificationCenter.default.addObserver(forName: .updateVIPState, object: nil, queue: nil) { noti in
            if GlobalTool.userModel?.isVip.intValue ?? 0 > 0 {
                self.navView.frame = CGRect(x: 0, y: 0, width: .screenWidth, height: 260)
            }else{
                self.navView.frame = CGRect(x: 0, y: 0, width: .screenWidth, height: 310)
            }
            self.tableView.reloadData()
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        loadData()        
    }
    
    private func loadData(){
        
        request.start(.RefreshUserModel, type: AIGirlUserModel.self, isLoading: false) { [weak self] result in
            if result.isSuccessful {
                GlobalTool.userModel = result.model
                if result.model?.isVip.intValue ?? 0 > 0 {
                    self?.navView.frame = CGRect(x: 0, y: 0, width: .screenWidth, height: 260)
                }else{
                    self?.navView.frame = CGRect(x: 0, y: 0, width: .screenWidth, height: 310)
                }
                self?.navView.updateUserInfo()
                self?.tableView.reloadData()
            }
        }
    }
}

extension AIGirlProfileViewController: UITableViewDelegate, UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.cell(AIGirlProfileTableViewCell.self, indexPath: indexPath)
        if indexPath.section == 0 {
            cell.itemInfos = [.like,.get]
        }else{
            cell.itemInfos = [.term,.privacy,.account,.clear]
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if indexPath.section == 0 {
            return 120
        }else{
            return 240
        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerID) as! AIGirlProfileHeaderView
        return header
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

        return 15.Scale
    }
    
}
